package com.summer.utils.commen;


import com.summer.utils.json_serialization.JsonMapper;
import org.springframework.util.StringUtils;

public class JsonCommonResponse<T> {

    private Integer code;

    private String msg;

    private boolean success;

    private T data;

    private static JsonMapper mapper = JsonMapper.nonDefaultMapper();

    public static <T> JsonCommonResponse<T> succJessRes() {
        JsonCommonResponse<T> jcr = new JsonCommonResponse<T>();
        jcr.setCode(1);
        jcr.setSuccess(true);
        return jcr;
    }

    public static <T> JsonCommonResponse<T> succJessRes(T data) {
        JsonCommonResponse<T> jcr = new JsonCommonResponse<T>();
        jcr.setCode(1);
        jcr.setSuccess(true);
        jcr.setData(data);
        return jcr;
    }
    
    public static <T> JsonCommonResponse<T> succJessRes(T data, String msg) {
        JsonCommonResponse<T> jcr = new JsonCommonResponse<T>();
        jcr.setCode(1);
        jcr.setSuccess(true);
        jcr.setData(data);
        jcr.setMsg(msg);
        return jcr;
    }

    public static <T> JsonCommonResponse<T> failRes(String msg) {
        JsonCommonResponse<T> jcr = new JsonCommonResponse<T>();
        jcr.setCode(0);
        jcr.setSuccess(false);
        jcr.setMsg(msg);
        return jcr;
    }

    public static <T> JsonCommonResponse<T> failRes() {
        JsonCommonResponse<T> jcr = new JsonCommonResponse<T>();
        jcr.setCode(0);
        jcr.setSuccess(false);
        return jcr;
    }

    public static <T> JsonCommonResponse<T> failRes(Integer code) {
        JsonCommonResponse<T> jcr = new JsonCommonResponse<T>();
        jcr.setCode(code);
        jcr.setSuccess(false);
        return jcr;
    }

    public static <T> JsonCommonResponse<T> failRes(Integer code, String msg) {
        JsonCommonResponse<T> jcr = new JsonCommonResponse<T>();
        jcr.setCode(code);
        jcr.setMsg(msg);
        jcr.setSuccess(false);
        return jcr;
    }

    public static <T> JsonCommonResponse<T> failRes(String msg, T data) {
        JsonCommonResponse<T> jcr = new JsonCommonResponse<T>();
        jcr.setCode(0);
        jcr.setMsg(msg);
        jcr.setData(data);
        jcr.setSuccess(false);
        return jcr;
    }

    public static <T> JsonCommonResponse<T> failRes(Integer code, String msg, T data) {
        JsonCommonResponse<T> jcr = new JsonCommonResponse<T>();
        jcr.setCode(code);
        jcr.setMsg(msg);
        jcr.setData(data);
        jcr.setSuccess(false);
        return jcr;
    }

    public JsonCommonResponse<T> setMsgResult(String msg, boolean success) {
        setMsg(msg);
        setSuccess(success);
        return this;
    }


    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public void appendMsg(String str) {
        if (StringUtils.isEmpty(msg)) {
            msg = str;
        } else {
            msg = msg + str;
        }
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String toString() {
        return mapper.toJson(this);
    }



}
